package org.chat.client.screens;

import org.chat.client.ChatService;
import org.chat.client.ChatServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ImageBundle;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.ImageBundle.Resource;

public class PanelChat  {

	private final ChatServiceAsync chatService = GWT
	.create(ChatService.class);

	private VerticalPanel panelChat , panelMessage;
	private DisclosurePanel disclosureOptionsChat;
	private static HorizontalPanel panelOptionsChat;
	ScrollPanel messageChat;
	private static VerticalPanel panelToolBar;

	RichTextArea  textBox ;
	private static RichTextToolbar toolbar;

	private static Button pieceJointe;
	private static Button smielys;
	private static Button historique;
	private static Button envoyer;

	/**
	 * Creates a new chat input area
	 */
	public PanelChat() {

		// panels
		panelChat = new VerticalPanel();
		disclosureOptionsChat = new DisclosurePanel("Editeur HTML");
		panelOptionsChat = new HorizontalPanel();
		panelToolBar = new VerticalPanel();
		messageChat = new ScrollPanel();

		panelMessage = new VerticalPanel();
		messageChat.setWidget(panelMessage);
		messageChat.getAbsoluteTop();

		// RichTextArea
		textBox = new RichTextArea ();
		toolbar = new RichTextToolbar(textBox);


		// Buttons
		pieceJointe = new Button("Joindre une pi\u00E8ce jointe");
		smielys = new Button("smielys");
		historique = new Button("Historique");
		envoyer = new Button("Envoyer");

		// Tab
		Button[] optionsChat = { pieceJointe, smielys,historique };
		Widget[] tabChatToolBar = {  textBox, envoyer };
		Widget[] tabChat = { messageChat,panelOptionsChat,disclosureOptionsChat,panelToolBar };

		/*
		 * Add to panels
		 */

		for (Button btn : optionsChat) {
			panelOptionsChat.add(btn);
		}
		disclosureOptionsChat.add(toolbar);

		for (Widget wg : tabChatToolBar) {
			panelToolBar.add(wg);
		}

		for (Widget wg : tabChat) {
			panelChat.add(wg);
		}

		/*
		 * Header of disclosure panels
		 */

		this.gestionOpenClose();

		/*
		 * Style css
		 */

		panelChat.addStyleName("pnlChat");
		messageChat.addStyleName("pnlMessageChat");
		disclosureOptionsChat.addStyleName("pnldisOptionsChat");
		panelOptionsChat.addStyleName("pnlBtnOptionsChat");
		panelToolBar.addStyleName("pnlChatToolBar");
		textBox.addStyleName("rtaChat");
		pieceJointe.addStyleName("btnChatPieceJointe");
		historique.addStyleName("btnChatHistorique");
		smielys.addStyleName("btnChatSmielys");
		envoyer.addStyleName("btnChatEnvoyer"); 

		//action listner
		MyHandler handler = new MyHandler();
		envoyer.addClickHandler(handler);
		textBox.addKeyDownHandler(handler);



	}


	private void gestionOpenClose() {

		disclosureOptionsChat.addOpenHandler(new OpenHandler<DisclosurePanel>() {
			public void onOpen(OpenEvent<DisclosurePanel> event) {
				disclosureOptionsChat.getHeaderTextAccessor().setText(
				"Masquer les options du chat");
			}
		});

		disclosureOptionsChat.addCloseHandler(new CloseHandler<DisclosurePanel>() {
			public void onClose(CloseEvent<DisclosurePanel> event) {
				disclosureOptionsChat.getHeaderTextAccessor().setText(
				"Afficher les options du chat");
			}
		});

	}


	public VerticalPanel afficherComposant(boolean activer) {

		textBox.setEnabled(activer);
		envoyer.setEnabled(activer);
		pieceJointe.setEnabled(activer);
		return panelChat;
	}

	class MyHandler implements ClickHandler, KeyDownHandler {

		private Images images = (Images) GWT.create(Images.class);
		public void onClick(ClickEvent event) {
			if(!textBox.getHTML().isEmpty()){
				sendMessageToServer();
			}
			else{
				Window.alert("Vous n'avez ecrit aucun message");
			}
		}

		private void sendMessageToServer() {


			chatService.messageToServer(checkSmielysInMessage(textBox.getHTML()),
					new AsyncCallback<String>() {
				public void onFailure(Throwable caught) {
					// Show the RPC error message to the user

				}

				public void onSuccess(String result) {

					panelMessage.add(new HTML(result));
					//panelMessage.add(new HTML(result));
					textBox.setHTML("");
					textBox.setFocus(true);

				}
			});
		}

		private String checkSmielysInMessage(String message){

			message=replace(message,"lol", ""+images.cool().getHTML()+"");
			message=replace(message,";(", ""+images.cry().getHTML()+"");
			message=replace(message,":|", ""+images.embarassed().getHTML()+"");
			message=replace(message,":(", ""+images.frown().getHTML()+"");
			message=replace(message,":.", ""+images.innocent().getHTML()+"");
			message=replace(message,":h", ""+images.kiss().getHTML()+"");
			message=replace(message,":D", ""+images.laughing().getHTML()+"");
			message=replace(message,":$", ""+images.moneymouth().getHTML()+"");
			message=replace(message,":l", ""+images.sealed().getHTML()+"");
			message=replace(message,":)", ""+images.smile().getHTML()+"");
			message=replace(message,":o", ""+images.surprised().getHTML()+"");
			message=replace(message,":p", ""+images.tongueout().getHTML()+"");
			message=replace(message,":\\", ""+images.undecided().getHTML()+"");
			message=replace(message,";)", ""+images.wink().getHTML()+"");
			message=replace(message,"xo", ""+images.yell().getHTML()+"");


			return message;
		}

		@Override
		public void onKeyDown(KeyDownEvent event) {
			// TODO Auto-generated method stub
			//if (event.getNativeEvent().equals( KeyCodes.KEY_ENTER)) {
			if (event.getNativeKeyCode()== KeyCodes.KEY_ENTER) {
				if(!textBox.getHTML().isEmpty()){
					sendMessageToServer();
				}
				else{
					Window.alert("Vous n'avez ecrit aucun message");
				}
			}
		}

		//la metode de remplacement 
		public String replace(String originalText,
				String subStringToFind, String subStringToReplaceWith) {
			int s = 0;
			int e = 0;

			StringBuffer newText = new StringBuffer();

			while ((e = originalText.indexOf(subStringToFind, s)) >= 0) {

				newText.append(originalText.substring(s, e));
				newText.append(subStringToReplaceWith);
				s = e + subStringToFind.length();

			}

			newText.append(originalText.substring(s));
			return newText.toString();

		} // end replace(String, String, String)



	}

	@SuppressWarnings("deprecation")
	public interface Images extends ImageBundle {

		/**
		 * @ImageBundle cry.gif
		 */
		@Resource("org/chat/client/images/smileys/cry.gif")
		AbstractImagePrototype cry();

		/**
		 * @ImageBundle cool.gif
		 */
		@Resource("org/chat/client/images/smileys/cool.gif")
		AbstractImagePrototype cool();

		/**
		 * @ImageBundle embarassed.gif
		 */
		@Resource("org/chat/client/images/smileys/embarassed.gif")
		AbstractImagePrototype embarassed();

		/**
		 * @ImageBundle frown.gif
		 */
		@Resource("org/chat/client/images/smileys/frown.gif")
		AbstractImagePrototype frown();

		/**
		 * @ImageBundle innocent.gif
		 */
		@Resource("org/chat/client/images/smileys/innocent.gif")
		AbstractImagePrototype innocent();

		/**
		 * @ImageBundle kiss.gif
		 */
		@Resource("org/chat/client/images/smileys/kiss.gif")
		AbstractImagePrototype kiss();

		/**
		 * @ImageBundle laughing.gif
		 */
		@Resource("org/chat/client/images/smileys/laughing.gif")
		AbstractImagePrototype laughing();

		/**
		 * @ImageBundle money-mouth.gif
		 */
		@Resource("org/chat/client/images/smileys/money-mouth.gif")
		AbstractImagePrototype moneymouth();

		/**
		 * @ImageBundle sealed.gif
		 */
		@Resource("org/chat/client/images/smileys/sealed.gif")
		AbstractImagePrototype sealed();



		/**
		 * @ImageBundle smile.gif
		 */
		@Resource("org/chat/client/images/smileys/smile.gif")
		AbstractImagePrototype smile();

		/**
		 * @ImageBundle surprised.gif
		 */
		@Resource("org/chat/client/images/smileys/surprised.gif")
		AbstractImagePrototype surprised();

		/**
		 * @ImageBundle tongue-out.gif
		 */
		@Resource("org/chat/client/images/smileys/tongue-out.gif")
		AbstractImagePrototype tongueout();

		/**
		 * @ImageBundle undecided.gif
		 */
		@Resource("org/chat/client/images/smileys/undecided.gif")
		AbstractImagePrototype undecided();

		/**
		 * @ImageBundle wink.gif
		 */
		@Resource("org/chat/client/images/smileys/wink.gif")
		AbstractImagePrototype wink();

		/**
		 * @ImageBundle yell.gif
		 */
		@Resource("org/chat/client/images/smileys/yell.gif")
		AbstractImagePrototype yell();





	}


}
